#include <stdio.h>
#include <mpx_supt.h>


date_rec* dateP;
void paginate();
void display_file(char*);
void help(char* arg){
	int i;
	int arglen = strlen(arg);
	if(arglen != 0){
		display_file(strcat(arg, ".txt"));
	}
	else{
		display_file("HELP.txt");
	}
}

//make version contant global in header
void display_version(){
	printf("\nVersion 4.6\n\n");
}

void display_date(){
	sys_get_date(dateP);
	printf("\n%d/", dateP->month);
	printf("%d/", dateP->day);
	printf("%d\n\n", dateP->year);
}

void set_date(int m, int d, int y){
	int valid = 1;           //date validation parameter

	if(y<0) {valid=0;}    //Year is a negative number

	if(m>12 || m<1) {valid=0;} //Month must be 1-12

	if(m==2 && d>28) {valid=0;}  //28 days in feb

	else if((m==4 || m==6 || m==9 || m==11) && d>30) {valid=0;}//30 days

	else if((m==1 || m==3 || m==5 || m==7 || m==8
	|| m==10 || m==12) && d>31) {valid=0;}        //31 day months


	if(valid)           //Valid date, execute sys_set_date()
	{
		dateP->month = m;
		dateP->day = d;
		dateP->year = y;
		sys_set_date(dateP);
	}

	else if(valid==0)
	{ printf("Invalid date, please try again.\n");}
}

void date_cmd(char* date_str){
	int n;
	int p;
	int m;
	int d;
	int y;
	char cdate[10];
	const int arglen = strlen(date_str);
	if((date_str[0]=='-')&&(tolower(date_str[1])=='s')){
		n = 2;
		p = 0;
		while(n<arglen){
			if((date_str[n] != ' ')&&(date_str[n] != '\t')){
				cdate[p]=date_str[n];
				p++;
			}
			n++;
		}
		m = (((int)(cdate[0])-48)*10)+((int)(cdate[1])-48);
		d = (((int)(cdate[3])-48)*10)+((int)(cdate[4])-48);
		y = ((((int)(cdate[6])-48)*1000)+(((int)(cdate[7])-48)*100)+(((int)(cdate[8])-48)*10)+((int)(cdate[9])-48));
		set_date(m, d, y);
	}
	else{
		printf("\"%s\" is not a recognizable date argument.\n", date_str);
	}
}

void terminateMPX(){
	int buf_size=10;
	char inbuf[10];
	printf("Yeah? y/n ");
	sys_req(READ, TERMINAL, inbuf, &buf_size);
	if((inbuf[0]=='y')||(inbuf[0]=='Y')){
		sys_exit();
	}
	else{
		printf("Exit aborted.\n");
	}
}

void clear_screen(){
	int bs = 1;
	char buf[1];
	sys_req(CLEAR, TERMINAL, buf, &bs);
}
//removes all whitespace from a string
char* remove_ws(char* str){
	int strL = strlen(str);
	char str_new[80]={'\0'};
	int x;
	for(x=0; x<strL; x++){
		if(!((str[x]==' ')||(str[x]=='\t')||(str[x]=='\n'))){
			str_new[x] = str[x];
		}
	}
	strcpy(str, str_new);
	return str;
}

void list_dir(char *dir_path){
  //int buf_size = 20;
	int error_check;
	char file_name_buf[20];
	long *file_size_p;
	int x=1;
  //open directory specified
  
	if (!strcmp(dir_path, "")){
		error_check = sys_open_dir('\0');
	}
	else{
		error_check = sys_open_dir(dir_path); //look up directory to open 
	}
	
	if((error_check == ERR_SUP_DIRNOP)||(error_check == ERR_SUP_INVDIR)){
		printf("Error opening directory: %s\n", dir_path);
	}
	else{
		do{
			if(x%10==0){
				paginate();
			}
			error_check = sys_get_entry(file_name_buf, 20, file_size_p);
			printf("%s \t\t %d bytes\n",file_name_buf, file_size_p);
			x++;
		} while (error_check != ERR_SUP_NOENTR);
	}
  //close directory
  sys_close_dir();

}

void paginate(){
	char wait_buf[2]={'\0'};
	int wbuf_size=2;
	printf("Press enter to continue.\n");
	while(wait_buf[0]!='\n'){
		sys_req(READ, TERMINAL, wait_buf, &wbuf_size);
	}
	clear_screen();
}

/**
***following code is for loading a file and printing it to terminal
**/
void display_file(char* file_name){
	FILE *fptr;
	//strcat(file_name, ".txt");
	fptr=fopen(file_name,"r");
	if(fptr){
		while(!feof(fptr)){
			printf("%c", fgetc(fptr));
		}
		printf("\n");
	}
	else{
		printf("Could not find %s file in current directory.\n", file_name);
	}
	fclose(fptr);
}


//string to int converter
int strint(char* str){
	int num = 0;
	int s_size = strlen(str);
	int i=0;
	int nflag = 1;
	if(str[i] == '-'){
		nflag = -1;
		i++;
	}
	while(i<s_size){
		//printf("%c strint num: %d\n", str[i], num);
		num = (int)(str[i])-48 + num*10;
		i++;
	}
	return num*nflag;
}
	